<?php

    use app\admin\model\AdminLog;
    use app\admin\model\Setting;
    use app\admin\model\Upload;
    use service\JsonService;
    use service\TimeService;
    use service\TreeService;
    use think\Exception;
    use think\Image;
    use think\Db;
    use think\Request;

    /**
     * Created by PhpStorm.
     * User: jackhhy
     * Date: 2019/6/26-14:14
     * Link: https://gitee.com/luckygyl/luckyAdmin
     * FileName: common.php
     * Keys: ctrl+alt+L/ctrl+s(代码格式化) ctrl+J(代码提示) ctrl+R(替换)ALT+INSERT(生成代码(如GET,SET方法,构造函数等) , 光标在类中才生效)
     * CTRL+ALT+O (优化导入的类和包 需要配置) SHIFT+F2(高亮错误或警告快速定位错误)
     * CTRL+SHIFT+Z(代码向前) CTRL+SHIFT+/ (块状注释) ctrl+shift+enter(智能完善代码 如if())
     *
     **************************************************************
     *                                                            *
     *   .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.       *
     *    |                     ______                     |      *
     *    |                  .-"      "-.                  |      *
     *    |                 /            \                 |      *
     *    |     _          |              |          _     |      *
     *    |    ( \         |,  .-.  .-.  ,|         / )    |      *
     *    |     > "=._     | )(__/  \__)( |     _.=" <     |      *
     *    |    (_/"=._"=._ |/     /\     \| _.="_.="\_)    |      *
     *    |           "=._"(_     ^^     _)"_.="           |      *
     *    |               "=\__|IIIIII|__/="               |      *
     *    |              _.="| \IIIIII/ |"=._              |      *
     *    |    _     _.="_.="\          /"=._"=._     _    |      *
     *    |   ( \_.="_.="     `--------`     "=._"=._/ )   |      *
     *    |    > _.="                            "=._ <    |      *
     *    |   (_/                                    \_)   |      *
     *    |                                                |      *
     *    '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='      *
     *                                                            *
     *                    JUST FIND YOUR BUG !                    *
     **************************************************************
     */


    /**
     * @param $arrs
     * @return bool|int
     * @author: Jackhhy <jackhhy520@qq.com>
     * @name: AddLogs
     * @describe: 日志
     */
     function addLogs($arrs){
        $set=new Setting();
        $res=$set->getSetting();
        if((int)$res['safe']['admin_log']==2){ //关闭后台日志
            return true;
        }
         $log=new AdminLog();
         return $log->allowField(true)->save($arrs);
}






    /**
     * @param $filename
     * @return string
     * @author: Jackhhy <jackhhy520@qq.com>
     * @name: fileext
     * @describe:获取文件后缀名
     */
    function fileext($filename) {
        return strtolower(trim(substr(strrchr($filename, '.'), 1, 10)));
    }

    /**
     * @param $filepath
     * @param string $filename
     * @author: Jackhhy <jackhhy520@qq.com>
     * @name: file_down
     * @describe: 文件下载
     */
    function file_down($filepath, $filename = '') {
        if(!$filename) $filename = basename($filepath);
        if(is_ie()) $filename = rawurlencode($filename);
        $filetype = fileext($filename);
        $filesize = sprintf("%u", filesize($filepath));
        if(ob_get_length() !== false) @ob_end_clean();
        header('Pragma: public');
        header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
        header('Cache-Control: no-store, no-cache, must-revalidate');
        header('Cache-Control: pre-check=0, post-check=0, max-age=0');
        header('Content-Transfer-Encoding: binary');
        header('Content-Encoding: none');
        header('Content-type: '.$filetype);
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        header('Content-length: '.$filesize);
        readfile($filepath);
        exit;
    }


    /**
     * @param $content
     * @return mixed|string
     * @author: Jackhhy <jackhhy520@qq.com>
     * @name: match_img
     * @describe: 获取内容中的图片
     */
    function match_img($content){
        preg_match('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/', $content, $match);
        return !empty($match) ? $match[1] : '';
    }


    /**
     * @param $content
     * @param string $targeturl
     * @return bool|mixed
     * @author: Jackhhy <jackhhy520@qq.com>
     * @name: grab_image
     * @describe:获取远程图片并把它保存到本地, 确定您有把文件写入本地服务器的权限
     */
    function grab_image($content, $targeturl = ''){
        preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/', $content, $img_array);
        $img_array = isset($img_array[1]) ? array_unique($img_array[1]) : array();

        if($img_array) {
            $path =  '/uploads/'.'grap_image/'.date('Ym/d');
            $urlpath = ROOT_PATH.$path;
            $imgpath =  UPLOAD_PATH.$path;
            if(!is_dir($imgpath)) @mkdir($imgpath, 0777, true);
        }

        foreach($img_array as $key=>$value){
            $val = $value;
            if(strpos($value, 'http') === false){
                if(!$targeturl) return $content;
                $value = $targeturl.$value;
            }
            $ext = strrchr($value, '.');
            if($ext!='.png' && $ext!='.jpg' && $ext!='.gif' && $ext!='.jpeg') return false;
            $imgname = date("YmdHis").rand(1,9999).$ext;
            $filename = $imgpath.'/'.$imgname;
            $urlname = $urlpath.'/'.$imgname;

            ob_start();
            readfile($value);
            $data = ob_get_contents();
            ob_end_clean();
            file_put_contents($filename, $data);

            if(is_file($filename)){
                $content = str_replace($val, $urlname, $content);
            }else{
                return $content;
            }
        }
        return $content;
    }



